using System.Collections;

namespace ETestIT.DataAccess
{
    using System;
    using System.Collections.Generic;
    using Domain.Entities;
    using Domain.Repositories;
    using Repository;

    public class JobTitleRepositoryImpl : IJobTitleRepository
    {
        public IList<JobTitle> GetAll()
        {
            const string query = @"SELECT Id , Name FROM JobTitles";

            return new FluentCommand<JobTitle>(query)
                .SetMap(JobTitleMap)
                .AsList();
        }

        public void Add(JobTitle jobTitle)
        {
            const string query = @"insert into JobTitles values(@Id,@Name)";

            jobTitle.Id = Guid.NewGuid();
            new FluentCommand<JobTitle>(query)
                .AddGuid("Id", jobTitle.Id)
                .AddString("Name", jobTitle.Name)
                .AsNonQuery();
        }

        public void Update(JobTitle jobTitle)
        {
            const string query = @"update JobTitles set Name=@Name where Id=@Id";
            new FluentCommand<JobTitle>(query)
                .AddGuid("Id", jobTitle.Id)
                .AddString("Name", jobTitle.Name)
                .AsNonQuery();
        }
        
        public void Delete(Guid id)
        {
            const string query = @"delete from JobTitles where Id=@Id";
            new FluentCommand<JobTitle>(query)
                .AddGuid("Id", id)
                .AsNonQuery();
        }

        public JobTitle GetById(Guid id)
        {
            const string sql = @"SELECT Id, Name FROM JobTitles WHERE Id = @Id";

            return new FluentCommand<JobTitle>(sql)
                .AddGuid("Id", id)
                .SetMap(JobTitleMap)
                .AsSingle();
        }

        private JobTitle JobTitleMap(DataReader reader)
        {
            return new JobTitle()
                       {
                           Id = reader.GetGuid("Id"),
                           Name = reader.GetString("Name") 
                       };
        }

       

    }
}